package com.cgbl.cg.vm.mine

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import api.UserServiceFactory
import com.alibaba.fastjson.JSON
import com.cgbl.cg.utils.city.BeanTransferHelper
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.configs.PrefsManager
import com.robot.base.model.PageListBean
import com.robot.base.model.QuestionBean
import com.robot.base.model.UserInfoBean
import com.robot.base.model.VipBean
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse
import me.goldze.mvvmhabit.utils.StringUtils

class OpenVipViewModel(application: Application) : GLBaseViewModel(application) {

    val quanyi = ObservableField<String>()
    val vipName = ObservableField<String>()
    val id = ObservableField<String>()
    val price = ObservableField<String>()
    val tabType = ObservableInt(TAB_MONTH)
    val priceText = ObservableField("确认支付")

    val uc = UiChangeEvent()

    class UiChangeEvent {
        val vipEvent = SingleLiveEvent<List<VipBean>>()
        val questionEvent = SingleLiveEvent<List<QuestionBean>>()
    }

    val clickConfirmOrderCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.Mine.A_MINE_CONFIRM_ORDER,
            Bundle().apply {
                putString(
                    AppConstants.BundleKey.SHOP_CART_BEAN, JSON.toJSONString(
                        BeanTransferHelper.createConfirmOrderProductByVip(
                            id.get(), price.get(), vipName.get()
                        )
                    )
                )
            })
    })


    val nickNameField = ObservableField<String>()

    val engineerStrField = ObservableField<String>()

    val hasEngineerField = ObservableBoolean()

    val overallIntegralField = ObservableField<String>()

    val fansCountField = ObservableField<String>()

    val followCountField = ObservableField<String>()

    fun refreshUserInfo() {
        UserServiceFactory.userInfo()
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<UserInfoBean>>(this) {
                override fun onNext(t: BaseResponse<UserInfoBean>) {
                    PrefsManager.saveUserInfo(t.data)
                    refreshUserInfoData(t.data)
                }
            })
    }

    fun getVipList() {
        UserServiceFactory.getVipList()
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<VipBean>>>(this) {
                override fun onNext(t: BaseResponse<List<VipBean>>) {
                    uc.vipEvent.postValue(t.data)
                    if (!t.data.isEmpty()) {
                        quanyi.set(t.data[0].quanyi)
                        vipName.set(t.data[0].cardName)
                        id.set(t.data[0].id)
                        price.set(t.data[0].cardPrice)
                        priceText.set("确认支付￥${t.data[0].cardPrice}")
                    }
                }
            })
    }

    fun qaPage() {
        UserServiceFactory.qapage(
            mapOf(
                "pageNo" to 1, "pageSize" to 100, "typeId" to 0
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<QuestionBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<QuestionBean>>) {
                uc.questionEvent.postValue(t.data.list)
            }
        })
    }

    private fun refreshUserInfoData(userInfoBean: UserInfoBean) {
        nickNameField.set(userInfoBean.nickName)
        engineerStrField.set(userInfoBean.engineer)
        hasEngineerField.set(!StringUtils.isEmpty(userInfoBean.engineer))
        overallIntegralField.set(userInfoBean.overallIntegral)
        fansCountField.set(userInfoBean.fansCount)
        followCountField.set(userInfoBean.followCount)
    }

    companion object {

        const val TAB_MONTH = 1

        const val TAB_THREE_MONTH = 2

        const val TAB_YEAR = 3

    }
}